package com.jack.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jack.mapper.TestPersonMapper;
import com.jack.domain.model.TestPerson;
import com.jack.util.GsonUtil;
import com.jack.util.RedisUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * Created by LiJiakui on 17/4/20.
 */
@RestController
@RequestMapping("/person")
public class PersonController {

    private static final Logger logger = Logger.getLogger(PersonController.class);

    @Autowired
    private TestPersonMapper testPersonMapper;

    @RequestMapping("/selectById")
    public TestPerson selectById(Integer id){
        return testPersonMapper.selectByPrimaryKey(id);
    }

    @RequestMapping("/insert")
    public String insertPerson(){
        try {
            TestPerson person = new TestPerson();
            person.setNickName(new Date().toString());
            testPersonMapper.insert(person);
            return "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping("/update")
    public String update(Integer id){
        try {
            TestPerson person = new TestPerson();
            person.setId(id);
            person.setNickName(new Date().toString());
            testPersonMapper.updateByPrimaryKeySelective(person);
            return "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        try {
            testPersonMapper.deleteByPrimaryKey(id);
            return "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping("/selectAll")
    public  PageInfo selectAll(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<TestPerson> list = testPersonMapper.selectAll();
        RedisUtil.setJedisVal("test", GsonUtil.toJson(list));
        logger.info("redis --- 值 : " + RedisUtil.getJedisVal("test"));
        PageInfo page = new PageInfo(list);
        return page;
    }
}
